﻿using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.DependencyInjection;
using System;
namespace Zeus.Mvc.Results.Implementations
{
    using Resources;
    public class RequestGuidZeusResultFormatter : IZeusResultFormatter
    {
        public object Formatter(ActionContext actionContext, object data)
        {
            var services = actionContext.HttpContext.RequestServices;
            var zeusResultData = services.GetService<IZeusResultData>() ?? new ZeusResultData();
            zeusResultData.GuidRequest = actionContext.HttpContext.HttpCorrelationId();
            if (data is Exception)
            {
                var exception = data as Exception;
                zeusResultData.Code = (int)ResultStatusCode.UnknowError;
                zeusResultData.Message = exception.Message;
                zeusResultData.Data = null;
            }
            else if (data is IZeusResultExceptionData)
            {
                var zeusResultExceptionData = data as IZeusResultExceptionData;
                zeusResultData.Code = zeusResultExceptionData.Code;
                zeusResultData.Message = zeusResultExceptionData.Message;
                zeusResultData.Data = null;
            }
            else
            {
                zeusResultData.Code = (int)ResultStatusCode.Success;
                zeusResultData.Data = data;
                zeusResultData.Message = string.Empty;
            }
            return zeusResultData;
        }
    }
}
